import { CameraRoll, ToastAndroid, Platform } from 'react-native';
import { writeFile, CachesDirectoryPath, downloadFile } from 'react-native-fs';
import { show } from '../constants/Toast';

//保存图片到本地相册的方法
export const saveImage = (url) => {
    console.log(CachesDirectoryPath)

    let temp = url.split("/");
    let ImageName = temp[temp.length - 1];
    if (Platform.OS == 'android') {
        var path = CachesDirectoryPath + "/" + ImageName;
        console.log(path);

        let options = {
            fromUrl: url,
            toFile: path,
            background: true,
        };
        downloadFile(options).promise.then((response) => {
            if (response.statusCode == 200) {
                let promise = CameraRoll.saveToCameraRoll(path, 'photo');
                show('保存成功')

                // promise.then(function (result) {
                //     console.log('error', result);
                //     BrightnessModule.reshPhoto(result);
                // }).catch(function (error) {
                //     console.log('error', error);
                //     show('二维码保存失败')
                // });
            } else {
                show('保存失败')
            }
        })
            .catch((err) => {

            })
    } else {
        let dirs = CachesDirectoryPath;

        const downloadDest = `${dirs}/${ImageName}`;

        console.log("downloadDest=--==-" + downloadDest)

        let options = {
            fromUrl: url,
            toFile: downloadDest,
            background: true,
        };
        downloadFile(options).promise.then((response) => {
            if (response.statusCode == 200) {
                console.log("移动至相册=--=-=")
                console.log("downloadDest=--==-" + downloadDest)
                var promise = CameraRoll.saveToCameraRoll(downloadDest, 'photo');

                console.log('console.log(promise)=--=-=')

                console.log(promise)

                promise.then(function (result) {
                    show('保存成功')
                }).catch(function (error) {
                    console.log('error', error);
                    show('保存失败')
                });
            } else {
                show('保存失败')
            }
        })
            .catch((err) => {

            })
    }
}